<?php
    define ( "ULTIMA_DIR", "/var/www/uo_zuluhotel/" );
    include_once("inc.mulutils.php");
    //session_start();
    if(isset($_REQUEST["action"])) {
        if ($_REQUEST["action"]=="load") {
            if(isset($_REQUEST["static_no"])) {
                $static_id = intval( $_REQUEST["static_no"] );
                $animdata = ReadAnimData($static_id);                 
            }           
        } 
        elseif($_REQUEST["action"]=="save") {   
            if(!isset($_REQUEST["static_no"])) {
                die("no static number specified!");
            }       
            $static_id = intval( $_REQUEST["static_no"] );             
            $animdata = LoadAnimDataFromRequest();
            
            WriteAnimData( $static_id, $animdata );                   
            $animdata = ReadAnimData($static_id); 
        }    
        elseif($_REQUEST["action"]=="delete_frame") {   
            if(!isset($_REQUEST["static_no"])) {
                die("no static number specified!");
            }                              
            $static_id = intval( $_REQUEST["static_no"] );             
            $animdata = LoadAnimDataFromRequest();
            if( $animdata["frames_no"] > 0 ) {                
                $animdata["frames"][$animdata["frames_no"]-1] = 0;
                $animdata["frames_no"] = $animdata["frames_no"] - 1;
            }
        }    
        elseif($_REQUEST["action"]=="add_frame") {   
            if(!isset($_REQUEST["static_no"])) {
                die("no static number specified!");
            }       
            $static_id = intval( $_REQUEST["static_no"] );                         
            $animdata = LoadAnimDataFromRequest();                   
            if( $animdata["frames_no"] < 64 ) {    
                $animdata["frames_no"] = $animdata["frames_no"] + 1;
                $animdata["frames"][$animdata["frames_no"]-1] = $animdata["frames_no"] - 1;
            }
        } 
    }
?>

<script type="text/javascript">
    function getFrameOffset() {
         var selected = $("#select_frame").find(":selected").text();
         selected = parseInt(selected);         
         $("#frame_offset").attr('disabled', false);
        $("#frame_offset").val(frames_data[selected]);
    }
    function SetFrameOffset() {
        var selected = $("#select_frame").find(":selected").text();
        if(!selected) {
            $("#frame_offset").attr('disabled', true);
            $("#frame_offset").val("no selection")
            return;
        }
        selected = parseInt(selected);  
        
        var new_value = $("#frame_offset").val();
        new_value = parseInt( new_value );
        if(isNaN(new_value))
        {
            alert("frame offset must be integer");
        }
        frames_data[selected] = new_value;
        $("#frames").val(JSON.stringify(frames_data));        
    }
</script>

<?php
    include_once("tpl.header.php");
?>    
    <form name="name" action="" method="post">
        
    <?php
        if(isset($animdata)) {
            echo "<input type='hidden' name='frames' id='frames' value='".json_encode($animdata["frames"])."'>";
        }
    ?> 
        
    <div>        
        
    <div style="float: left">
        
        <div class="group" style="display: inline-block; margin-bottom: 10px;">
        <div class="title">Tile:</div>
        <img src="megamul?static=<?php if(isset($static_id)) echo $static_id; ?>&transparent=1" >
        </div>
        <div class="group">
        <div class="title">Animation edit:</div>
    <label for="static_no">Static index:&nbsp;</label><input type="text" name="static_no" value="<?php echo isset($static_id)?$static_id:""; ?>" id="static_no"/>
        <br>
    <label for="start_interval">Start delay:&nbsp;</label><input type="text" name="start_interval" value="<?php if(isset($animdata)) { echo $animdata['start_interval']; } ?>" id="start_interval"/>
        <br>
    <label for="frame_interval">Frame delay:&nbsp</label><input type="text" name="frame_interval" value="<?php if(isset($animdata)) { echo $animdata['frame_interval']; } ?>" id="frame_interval"/>        
        </div>
            
    </div>
        <div class="group floatleft">
        <div class="title">Frames:</div>
    <select id="select_frame" size="6" style="width: 150px" onchange="getFrameOffset()">
        <?php                     
            if(isset($animdata)) {                
                for ($i=0; $i < $animdata["frames_no"]; $i++) { 
                    echo '<option value="'.$animdata["frames"][$i].'">'.$i.'</option>';
                }
            }        
        ?>        
    </select> 
        
        <br><br>
        <label for="frame_offset">Frame Offset: </label><input type="text" name="frame_offset" value="" size="5" onkeyup="SetFrameOffset()" id="frame_offset">
            <br>
        <label for="frame_count">Frame Count: </label><input type="text" name="frame_count" readonly size="5" value="<?php if(isset($animdata)) { echo $animdata['frames_no']; } ?>" id="frame_count"/>            
        <br><br>
        <button name="action" value="add_frame" type="submit">Add frame</button>            
        <button name="action" value="delete_frame" type="submit">Rem. frame</button>
        </div>
                 
        <div style="clear:both">    
    <button name="action" value="load" type="submit">Load data ..</button>
    <button name="action" value="save" type="submit">Save ..</button>    
        </div>            
    </div>
                       
    </form>
    
<script type="text/javascript">   
    var frames_data_encoded = $("#frames").val();
    var frames_data = jQuery.parseJSON(frames_data_encoded);
</script>
    
<?php
function LoadAnimDataFromRequest()
{    
    $animdata = array();
    $animdata["frames"] = json_decode($_REQUEST["frames"]);
    $animdata["frames_no"] = intval($_REQUEST["frame_count"]);
    
    $animdata["frame_interval"] = intval($_REQUEST["frame_interval"]);
    $animdata["start_interval"] = intval($_REQUEST["start_interval"]);   
    return $animdata;    
}
?>        
    
    </body>
</html>